import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

// 导入Element Plus图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 彻底解决ResizeObserver问题 - 最激进的方案
if (typeof window !== 'undefined') {
  // 1. 完全禁用ResizeObserver
  if (window.ResizeObserver) {
    const OriginalResizeObserver = window.ResizeObserver
    window.ResizeObserver = class MockResizeObserver extends OriginalResizeObserver {
      constructor(callback) {
        super(() => {
          // 完全忽略所有回调
          return
        })
        this._callback = callback
      }
      
      observe() {
        // 不执行任何观察
        return
      }
      
      unobserve() {
        // 不执行任何取消观察
        return
      }
      
      disconnect() {
        // 不执行任何断开连接
        return
      }
    }
  }
  
  // 2. 重写console方法，完全忽略ResizeObserver错误
  const originalError = console.error
  const originalWarn = console.warn
  
  console.error = (...args) => {
    const message = args[0]
    if (typeof message === 'string' && 
        (message.includes('ResizeObserver') || 
         message.includes('ResizeObserver loop') ||
         message.includes('ResizeObserver loop limit exceeded'))) {
      return // 完全忽略
    }
    originalError.apply(console, args)
  }
  
  console.warn = (...args) => {
    const message = args[0]
    if (typeof message === 'string' && 
        (message.includes('ResizeObserver') || 
         message.includes('ResizeObserver loop') ||
         message.includes('ResizeObserver loop limit exceeded'))) {
      return // 完全忽略
    }
    originalWarn.apply(console, args)
  }
  
  // 3. 全局错误处理器
  window.addEventListener('error', (event) => {
    if (event.message && 
        (event.message.includes('ResizeObserver') || 
         event.message.includes('ResizeObserver loop') ||
         event.message.includes('ResizeObserver loop limit exceeded'))) {
      event.preventDefault()
      event.stopPropagation()
      event.stopImmediatePropagation()
      return false
    }
  }, true)
  
  // 4. Promise拒绝处理器
  window.addEventListener('unhandledrejection', (event) => {
    if (event.reason && 
        ((typeof event.reason === 'string' && 
          (event.reason.includes('ResizeObserver') || 
           event.reason.includes('ResizeObserver loop'))) ||
         (event.reason.message && 
          (event.reason.message.includes('ResizeObserver') || 
           event.reason.message.includes('ResizeObserver loop'))))) {
      event.preventDefault()
      event.stopPropagation()
      event.stopImmediatePropagation()
      return false
    }
  })
  
  // 5. 重写Element Plus的ResizeObserver相关方法
  if (window.ElementPlus) {
    const originalEmit = window.ElementPlus?.emitter?.emit
    if (originalEmit) {
      window.ElementPlus.emitter.emit = function(event, ...args) {
        if (event && event.includes && event.includes('resize')) {
          return // 忽略resize事件
        }
        return originalEmit.apply(this, [event, ...args])
      }
    }
  }
}

const app = createApp(App)

// 注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(store)
app.use(router)
app.use(ElementPlus, {
  locale: zhCn,
})

app.mount('#app') 